<template>
  <div class="content-main">
    <div>
      <el-form :inline="true" :model="form" ref="form">
        <el-form-item label="员工名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
    </div>
    <div class="content-search">
      <el-button class="btnStyle" icon="el-icon-search" @click="search">查询</el-button>
      <el-button icon="el-icon-refresh-right" @click="reset">重置</el-button>
    </div>
    <div class="content-edit">
      <el-button class="btnStyle" icon="el-icon-plus" @click="add">添加</el-button>
    </div>
    <el-row class="content_row">
      <el-col :span="5">
        <TreeArea></TreeArea>
      </el-col>
      <el-col :span="19">
        <table-area
          :loading="loading"
          :page="page"
          :size="size"
          :total="total"
          :list="list"
          @current-change="currentChange"
          @size-change="sizeChange"
          @edit="edit"
          @del="del"
          @edit-role="editRole"
        />
      </el-col>
    </el-row>
    <edit-modal :show="editModal" :info="editModalInfo" @close="close" />
    <role-modal :info="roleModal" @close="editRole" />
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
// lib
import PageTableVm from "@/public/base/PageTableVm";
// components & widgets
import TableArea from "./TableArea.vue";
import EditModal from "./EditModal.vue";
import RoleModal from "./RoleModal.vue";
import TreeArea from "./TreeArea.vue";
// config
// script & public
import logUtils from "@/utils/logUtils";
// controller & service
import { delAdminService, getAdminPageService } from "@/biz/admin/service";
// interface
import { Admin } from "@/biz/admin/model";
import { ROOT_ADMIN_ID } from "@/config/setting";
// 其它

@Component({
  components: {
    TableArea,
    EditModal,
    RoleModal,
    TreeArea,
  },
})
export default class SystemAdmin extends PageTableVm {
  form: {
    name: string;
  } = {
    name: "",
  };
  roleModal: Admin | null = null;
  mounted() {
    // this.getPage();
  }
  async getPage() {
    logUtils.info("搜索项", JSON.stringify(this.form));
    // 调用服务
    this.loading = true;
    const result = await getAdminPageService({
      pageNum: this.page,
      pageSize: this.size,
      sysUserName: this.form.name,
    });
    this.loading = false;
    if (result.payload) {
      this.list = result.payload.list;
      this.total = result.payload.total;
    }
    // 失败和异常
  }
  async del(row: Admin) {
    if (row.sysUserId === ROOT_ADMIN_ID) {
      this.$message.warning("不能删除根管理员");
      return;
    }
    const result = await delAdminService(row.sysUserId);
    // 成功
    if (result.payload) {
      this.$message.success("删除成功");
      this.getPage();
    }
    // 失败和异常
  }
  editRole(row: Admin | null) {
    if (row && row.sysUserId === ROOT_ADMIN_ID) {
      this.$message.warning("根管理员不需设置角色");
      return;
    }
    this.roleModal = row;
  }
}
</script>

<style lang="scss">
.content_row {
  margin-top: 20px;
}
</style>
